package com.spark.colliery.module.agency.fragment

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.alert
import com.spark.common.ext.toast
import com.spark.common.ext.wrapIO
import com.spark.common.model.AgencyTodoListModel
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.trouble.HTAgencyActivity
import kotlinx.android.synthetic.main.frag_agency_todo.*

/**
 * 待办任务
 * @info 状态
 * 1 -> 标准化检查 - 主任务
 * 2 -> 标准化检查 - 子任务
 * 3 -> 借阅申请
 * 4 -> 文件传输
 * 5 -> 归档申请
 * 6 -> 现场治理
 * 7 -> 隐患子任务
 * 8 -> 现场治理
 * 9 -> 限时整改
 * 10 -> 挂牌督办
 * 11 -> 隐患升级
 * 12 -> 重大隐患
 */

class TodoFragment : BaseFragment() {
    private var currPage = 1
    private var totalPage = 0
    private lateinit var taskAdapter: AgencyAdapter

    companion object {
        fun newInstance(): TodoFragment {
            val fragment = TodoFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_agency_todo
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        taskAdapter = AgencyAdapter(R.layout.item_agency_task).apply {
            setOnItemClickListener { _, _, position ->
                val data = taskAdapter.data[position]
                LogUtils.e(data)
                if (data is AgencyTodoListModel.Data.Page.Result) {
                    when (data.taskTypeFlag) {
                    // 标准化检查
                        1, 2 -> {

                        }
                    // 借阅申请
                        3 -> {

                        }
                    // 文件传输
                        4 -> {
                        }
                    // 归档申请
                        5 -> {
                        }
                    // 隐患排查
                    // 现场治理
                    // 限时整改
                    // 挂牌督办
                    // 隐患升级
                    // 重大隐患
                        6, 7, 8, 9, 10, 11, 12 -> {
                            activity?.apply { HTAgencyActivity.actionStart(this, Gson().toJson(data)) }
                        }
                    }
                    /**
                     * (正在执行) 2 -> ChkActivity.actionStartForBack(this,ChkProgress.TO_ACCEPT,"${data.id}")
                     * (待接受) 5 -> ChkActivity.actionStartForBack(this,ChkProgress.To_ACCEPT,"${data.id}")
                     * (待检查) 7 -> ChkActivity.actionStartForBack(this,ChkProgress.TO_CHK,"${data.id}")
                     * (待审阅) 9 -> ChkActivity.actionStartForBack(this,ChkProgress.TO_CHK_APPR,"${data.id}")
                     */
                }
            }
        }
        recy_agency_tasks.apply {
            adapter = taskAdapter
            layoutManager = LinearLayoutManager(context)
        }
        refresh_layout.apply {
            setOnRefreshListener {
                currPage = 1
                fetchTodos(currPage)
                refresh_layout.finishRefresh()
            }
            setOnLoadmoreListener {
                currPage++
                if (currPage >= totalPage) {
                    alert("没有更多数据了")
                    it.finishLoadmore()
                } else {
                    fetchTodos(currPage)
                }
            }
        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        fetchTodos(currPage)
    }

    /**
     * 获取 todo 列表
     */
    private fun fetchTodos(page: Int) {
        // searchstr: 搜索关键词
        // taskType: 任务类型
        // state : 任务状态
        val parmas = mutableMapOf(
                Pair(Const.qr_page, "$page"),
                Pair(Const.qr_pageSize, "10"))
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_todo_task}", parmas.toJsonBody())
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, AgencyTodoListModel::class.java)?.apply {
                                    if (code == "200") {
                                        if (page == 1) {
                                            taskAdapter.data.clear()
                                            totalPage = this.data.page.totalPage
                                        }
                                        taskAdapter.addData(data.page.results)
                                    } else {
                                        toast(msg)
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                if (currPage == 1) refresh_layout.finishRefresh()
                                else refresh_layout.finishLoadmore()
                            },
                            {
                                if (currPage == 1) refresh_layout.finishRefresh()
                                else refresh_layout.finishLoadmore()
                            },
                            {
                                dispose = it
                            }
                    )
        }
    }
}

class AgencyAdapter(layoutRes: Int) : BaseQuickAdapter<AgencyTodoListModel.Data.Page.Result, BaseViewHolder>(layoutRes) {
    private val color_30c6d3: Int = Color.parseColor("#30c6d3")
    private val color_6053fa: Int = Color.parseColor("#6053fa")
    private val color_fea337: Int = Color.parseColor("#fea337")
    private val color_4ba5fb: Int = Color.parseColor("#4ba5fb")
    private val color_fc7828: Int = Color.parseColor("#fc7828")
    private val color_fd5249: Int = Color.parseColor("#fd5249")

    override fun convert(helper: BaseViewHolder?, item: AgencyTodoListModel.Data.Page.Result?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_agency_task_title, item.title)
            getView<TextView>(R.id.tv_state).apply {
                text = item.statesstr
                when (item.taskTypeFlag) {
                // 标化检查
                    1, 2 -> {
                        when (item.states) {
                        // 草稿
                            "1" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 待审核
                            "2" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 审核通过
                            "3" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 审核驳回
                            "4" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                                setTextColor(color_fd5249)
                            }
                        // 分配中
                            "5" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_fff1e9)
                                setTextColor(color_fc7828)
                            }
                        // 待检查
                            "6" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 检查中
                            "7" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                                setTextColor(color_fea337)
                            }
                        // 以评分
                            "8" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 审阅中
                            "9" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 待存储
                            "10" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                                setTextColor(color_fea337)
                            }
                        // 以存储
                            "11" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 已归档
                            "12" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        }
                    }
                // 隐患处理
                    6, 7 -> {
                        background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                        setTextColor(color_6053fa)
                    }
                    8 -> {
                        when (item.states) {
                        // 待处理
                            "1" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 审核中
                            "2" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 被驳回
                            "3" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                                setTextColor(color_fd5249)
                            }
                        // 待存储
                            "4" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                                setTextColor(color_fea337)
                            }
                        // 已存储
                            "5" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 已归档
                            "6" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        }
                    }
                    9, 10 -> {
                        when (item.states) {
                        // 待处理
                            "1" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 审核中
                            "2" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 被驳回
                            "3" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                                setTextColor(color_fd5249)
                            }
                        // 确认中
                            "4" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 整改中
                            "5" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_fff1e9)
                                setTextColor(color_fc7828)
                            }
                        // 验收中
                            "6" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 验收驳回
                            "7" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                                setTextColor(color_fd5249)
                            }
                        // 待存储
                            "8" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                                setTextColor(color_fea337)
                            }
                        // 以存储
                            "9" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 已归档
                            "10" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        }
                    }
                    11, 12 -> {
                        when (item.states) {
                        // 待处理
                            "1" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                                setTextColor(color_6053fa)
                            }
                        // 审核中
                            "2" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_ebf5fe)
                                setTextColor(color_4ba5fb)
                            }
                        // 被驳回
                            "3" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f4e7ea)
                                setTextColor(color_fd5249)
                            }
                        // 待存储
                            "4" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_f7f1e8)
                                setTextColor(color_fea337)
                            }
                        // 以存储
                            "5" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        // 已归档
                            "6" -> {
                                background = resources.getDrawable(R.drawable.bg_capsule_e7fcfe)
                                setTextColor(color_30c6d3)
                            }
                        }
                    }
                    else -> {
                        background = resources.getDrawable(R.drawable.bg_capsule_eae9fe)
                        setTextColor(color_6053fa)
                    }
                }
                setText(R.id.tv_agency_task_content, item.description)
                setText(R.id.tv_task_info, item.tasktype)
                setText(R.id.tv_agency_time, item.times)
            }
        }

    }
}
